package com.dream.keigezhushou.Activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.dream.keigezhushou.Activity.view.OnItemClickListener;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

/**
 * Created by mxmf-001 on 17/3/2.
 */
public class RecentPlayAdapter extends RecyclerView.Adapter<RecentPlayAdapter.MyViewHolder> {

    private View headView;
    private View footView;

    private static final int HEAD = 1;
    private static final int FOOT = 2;
    public static final int NORMAL = 3;


    private Context context;
    private ArrayList<String> rencentList;

    private OnItemClickListener itemMoreClickListener;

    public RecentPlayAdapter(Context context, ArrayList<String> recentList) {
        this.context = context;
        this.rencentList = recentList;
//        mInlater=LayoutInflater.from(context);


    }

    public void setOnItemClickListener(OnItemClickListener itemMoreClickListener) {
        this.itemMoreClickListener = itemMoreClickListener;
    }


    public void addHeaderView(View headerView) {
        this.headView = headerView;
    }

    public int getHeaderViewCount() {
        return headView == null ? 0 : 1;
    }

    public void addFootView(View footView) {
        this.footView = footView;
    }

    public int getFootViewCount() {
        return footView == null ? 0 : 1;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == HEAD) {
            return new MyViewHolder(headView);
        }
        if (viewType == FOOT) return new MyViewHolder(footView);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recentplay_rcv, null);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (getItemViewType(position) == HEAD) return;
        if (getItemViewType(position) == FOOT) return;

        holder.tvMusic.setText(rencentList.get(position - getHeaderViewCount()));
        holder.iv_more01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemMoreClickListener != null) {
                    itemMoreClickListener.onItemMoreClick(position);
                }


            }
        });

        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"播放全部",Toast.LENGTH_SHORT).show();


            }
        });
        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"删除全部",Toast.LENGTH_SHORT).show();


            }
        });



    }

    @Override
    public int getItemViewType(int position) {
        if (position < getHeaderViewCount()) return HEAD;
        if (position >= rencentList.size() + getHeaderViewCount()) return FOOT;


        return NORMAL;
    }


    @Override
    public int getItemCount() {


        return rencentList.size() + getHeaderViewCount() + getFootViewCount();

    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvMusic;
        TextView tvSinger;
        ImageView iv_more01;
        public LinearLayout Linearitem;
        Button btn_delete;


        public MyViewHolder(View view) {
            super(view);
            Linearitem = (LinearLayout) view.findViewById(R.id.item_music_list_recent);
            iv_more01 = (ImageView) view.findViewById(R.id.item_more);
            tvSinger = (TextView) view.findViewById(R.id.collected_music_singer);
            tvMusic = (TextView) view.findViewById(R.id.collected_music_name);
            btn_delete = (Button) view.findViewById(R.id.btn_from_delete);
        }
    }

    public void removeItem(int position) {
        rencentList.remove(position);
        notifyDataSetChanged();
    }
}
